<?php
/**
 * 推荐企业
 * @link Http://www.thankphp.Com
 */
class RecommendAction extends Action {
	/**
	 * 管理推荐类型
	 */
	public function AdminIndex(){
        EasyACL('Recommend','AdminIndex',$_SESSION);
		$Recommend = D('recommend');
        //计算所有的行数
        $count = $Recommend->count('','re_id');
        //每页显示数量
        $listRows = 10;
        $fields = 're_id,name';
        import('ORG.Util.Page');
        $p = new Page($count,$listRows);
        $list = $Recommend->findall(null,$fields,'re_id desc',$p->firstRow.','.$p->listRows);

        //执行分页
        $page = $p->show();
        //模板输出
        $this->assign('Res',$list);
        $this->assign('Page',$page);
        $this->display();
	}

	/**
	 * 创建新的推荐类型
	 */
	public function AdminCreate(){
        EasyACL('Recommend','AdminCreate',$_SESSION);
		if($this->isPost()){
            $Input = Input::getInstance();
            $Input->filter('addslashes');
            //获取运行时间
            $time = time();
            $Recommend = D('recommend');
            $Recommend->name = htmlspecialchars($Input->post('name'));
            $Recommend->created = $time;
            $Recommend->updated = $time;
            $Re = $Recommend->add();
            if(!$Re){
                halt('尝试创建新的类型失败');
            }
            $this->redirect('AdminIndex','Recommend');
        }

        $this->display();
	}

	/**
	 * 修改类型名称
	 */
	public function AdminUpdate(){
        EasyACL('Recommend','AdminUpdate',$_SESSION);
	    //判断是否POST提交
        if($this->isPost()){
            $Input = Input::getInstance();
            $Input->filter('addslashes');
            $re_id = $Input->post('re_id');
            $name = $Input->post('name');

            $Recommend = D('recommend');
            $Recommend->find($re_id);

            $Recommend->name = $name;
            $Recommend->updated = time();
            $Re = $Recommend->save();

            if(!$Re){
                halt('尝试修改编号为' . $re_id . '分类失败');
            }
            $this->redirect('AdminIndex','Recommend');
        }
        $Input = Input::getInstance();
        $Input->filter('addslashes');
        //如果不是POST提交，则进行查询并赋值
        //$re_id = (isset($_GET['re_id']))?(int)$_GET['re_id']:halt('没有获取到相应的传入值!');
        $Recommend = D('recommend');

        import('ORG.Util.HashMap');
        $conditions = new HashMap();
        $conditions->re_id = (int)$Input->get('re_id');

        $Res = $Recommend->where($conditions)->find();

        $this->assign('Res',$Res);
        $this->display();
	}

	/**
	 * 删除
	 */
	public function AdminDels(){
		EasyACL('Recommend','AdminDel',$_SESSION);

        //如果是POST传入值，则表示是删除多项内容
        if($this->isPost()){
            //赋值到变量
            $re_id = $_POST['re_id'];
            $Error = '';
            $Re = D('recommend');
            foreach($re_id as $id){
                $result = $Re->delete($id);
                if(!$result){
                    $Error .= '尝试删除编号为' . $id . '的推荐分类失败<br />';
                }
            }
            if(empty($Error)){
                $this->redirect('AdminIndex','Recommend');
            } else {
                halt($Error);
            }
        }

        //如果是GET传入值，则表示删除单项
        if($this->isGet()){
            $Input = Input::getInstance();
            $Input->filter('addslashes');
            $Re = D('recommend');
            $result = $Re->delete((int)$Input->get('re_id'));
            if(!$result){
                halt('尝试删除指定分类失败');
            }
            $this->redirect('AdminIndex','Recommend');
        }
	}
}
